<template>
<view>
<form @reset="formReset" @submit="release" reportSubmit="true">
    <view class="modle">
    车辆类型
    <text class="must_red">*</text>
        <picker @change="selectclassFun" class="modle_right" :range="classes" rangeKey="name" :value="index">
            <view>
        {{selectclass}}
      </view>
        </picker>
    </view>
    <view @tap="selectbrandFun" class="modle">
    车辆品牌
    <text class="must_red">*</text>
        <view class="modle_right">

      {{selectbrand}}

    </view>
    </view>
    <view class="modle">
    汽车名称
    <text class="must_red">*</text>
        <input class="modle_right" name="carname" placeholder="请输入汽车名称" placeholderStyle="color: #9295A8;" type="text" :value="defult_value">
    </view>
    <view class="modle">
    上牌日期
    <text class="must_red">*</text>
        <picker @change="bindDateChange" class="modle_right" fields="day" mode="date" start="2000-01-01" :value="date">
            <view>
         {{selecttime}}
      </view>
        </picker>
    </view>
    <view class="modle">
    公里数(单位万)
    <text class="must_red">*</text>
        <input class="modle_right" name="kilometre" placeholder="请输入公里数单位：万" placeholderStyle="color: #9295A8;" style="width:350rpx" type="digit" :value="defult_value">
    </view>
    <view class="modle">
    售价(单位万)
    <text class="must_red">*</text>
        <input class="modle_right" name="price" placeholder="请输入转让价格单位：万" placeholderStyle="color: #9295A8;" style="width:350rpx" type="digit" :value="defult_value">
    </view>
    <view class="modle">
    排气量
    <text class="must_red">*</text>
        <input class="modle_right" name="exhaust" placeholder="请输入排气量（如：1.6）T" placeholderStyle="color: #9295A8;" style="width:350rpx" :value="defult_value">
    </view>
    <view class="modle">
    变速箱
    <text class="must_red">*</text>
        <picker @change="gearbox" class="modle_right" mode="selector" :range="gearboxes" rangeKey="name" :value="index">
            <view>
        {{gearboxname}}
      </view>
        </picker>
    </view>
    <view class="modle">
    联系电话
    <text class="must_red">*</text>
        <button @getphonenumber="getPhoneNumber" class="modle_btn" open-type="getPhoneNumber">电话授权</button>
        <input class="modle_right" name="phone" placeholder placeholderStyle="color: #9295A8;" type="number" :value="userphone">
    </view>
    <view class="modle">
    车辆地址
      <text class="must_red">*</text>
        <button @tap="tomap" class="modle_btn">获取位置</button>
        <input class="modle_right" disabled="true" name="address" placeholder placeholderStyle="color: #9295A8;" style="width:350rpx;" type="text" :value="mapname&&mapname.length>2?mapname:''">
    </view>
    <view class="modle" style="border-bottom:0">
    车辆信息
    <button @tap="addcarsimgs" class="modle_btn">添加信息</button>
    </view>
    <view v-for="(item, index) in carsimgs" :key="index">
        <view class="shangchuan_view_border">
            <view class="shangchuan_view" v-if="item.img!=''">
                <image @tap="upthiscarsimgs" class="shangchuan_Img_close" :data-index="index" src="/static/pages/image/tupiangb_btn.png"></image>
                <image @tap="showimgs" class="shangchuan_Img" :data-index="index" :src="item.img"></image>
            </view>
            <view class="shangchuan_view" v-if="item.img==''">
                <image @tap="upcarsimgs" class="shangchuan_Img" :data-index="index" src="/static/pages/image/tianjiantp_btn.png"></image>
            </view>
        </view>
        <view class="car_messageView">
            <input @blur="setthisintro" class="car_messageView_input" :data-index="index" placeholder="请输入你的车辆信息" placeholderStyle="color: #9295A8;" type="text" :value="item.intro">
            <view @tap="delcarsimgs" class="car_messageView_sc" :data-index="index">删除</view>
        </view>
    </view>
    <button class="bufa_btn" style="background: #333;" v-if="status=='1'">已经禁止发布车辆信息</button>
    <button class="bufa_btn" form-type="submit" v-else>发布</button>
    <view class="jinggao">发布不真实车源，虚报价格，一经核查将做封号处理。</view>
</form>
<!--<import src="/pages/alert/index.wxml"></import>-->
<block data-type="template" data-is="alert" data-attr="isShow:isShow">
    <view class="wx_dialog_container" :hidden="!isShow">
        <view class="wx-alert-mask"></view>
        <view class="wx-alert-dialog">
            <view class="wx-alert-dialog-title">尊敬的用户</view>
            <view class="wx-alert-dialog-content">尚未获取到您的用户信息，较多功能无法体验，请允许获取用户信息</view>
            <view class="wx-alert-dialog-footer">
                <button @tap="hideDialog" class="wx-alert-dialog-btn">拒绝</button>
                <button @getuserinfo="updateUserInfo" class="wx-alert-dialog-btn" open-type="getUserInfo">允许</button>
            </view>
        </view>
    </view>
</block>
<button catchtouchmove="viewTouchMove" class="kefu_modle" :style="'top:' + top + 'px'">
    <button @tap="salefast" class="kuaisu_btn"></button>
    <image class="kefu_modle_img" src="/static/pages/image/kefu.png"></image>
</button>
</view>
</template>

<script>
var app = getApp(),
    upurl = app.globalData.util.url("entry/wxapp/Api", {
  m: "monai_sharing",
  uid: wx.getStorageSync("uid"),
  r: "Upimg"
});

export default {
  data() {
    return {
      upcarindex: 0,
      date: 0,
      carsimgs: [{
        img: "",
        intro: "",
        imgshort: ""
      }],
      brandid: "",
      brandname: "",
      classid: "",
      classes: [],
      brands: [],
      selectclass: "请选择分类",
      selectbrand: "请选择品牌",
      selecttime: " 请选择上牌日期",
      selecttime1: "",
      vehicletime: "请选择上牌日期",
      vehicletime1: "",
      insurance: "请选择保险到期时间",
      insurance1: "",
      gearboxes: [{
        name: "手自一体",
        id: "0"
      }, {
        name: "手动档",
        id: "1"
      }, {
        name: "自动挡",
        id: "2"
      }],
      gearboxname: "请选择变速箱类型",
      gearboxindex: 0,
      userphone: "",
      mapname: "",
      mapx: "",
      mapy: "",
      carimgs: [],
      buttscan: !1,
      notice: [],
      noticeindex: 0,
      textareatype: !1,
      textareaname: "请输入车辆信息介绍",
      textareainput: "",
      isShow: !1,
      showtextarea: !1,
      status: "",
      choose_index: 1,
      choose_active: 0,
      top: "0",
      defult_value: "",
      maxwidth: "",
      maxheight: ""
    };
  },

  components: {},
  props: {},
  onShow: function () {
    var a = this;
    console.log(wx.getStorageSync("userInfo")), "" != a.brandid && "" != a.brandname && a.setData({
      selectbrand: a.brandname
    });
  },
  onLoad: function (a) {
    var t = this;
    app.globalData.util.getUserInfo(function (a) {
      a.memberInfo || t.setData({
        isShow: !0
      }), app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          m: "monai_market",
          r: "me.info",
          uid: wx.getStorageSync("uid")
        },
        success: function (a) {
          console.log(a), 1 == a.data.data.status && wx.showModal({
            title: "提示",
            content: "您的用户已经禁止发布车辆信息，请联系管理员处理",
            showCancel: !1,
            success: function (a) {}
          }), t.setData({
            status: a.data.data.status
          });
        }
      });
    }), t.firstin(t);
  },
  methods: {
    intextarea: function () {
      this.setData({
        textareatype: !0,
        textareaname: this.textareainput
      });
    },
    viewTouchMove: function (a) {
      a.touches[0].clientY < 0 || 450 < a.touches[0].clientY || this.setData({
        top: a.touches[0].clientY - 30
      });
    },
    bindblur: function (a) {
      console.log(a.detail.value);
      var t = a.detail.value,
          e = t;
      t || (t = "请输入车辆信息介绍", e = ""), this.setData({
        textareatype: !1,
        textareaname: t,
        textareainput: e
      });
    },
    bindconfirm: function () {
      var a = new Date();
      console.log(a.getMilliseconds());
    },
    hideDialog: function () {
      this.setData({
        isShow: !this.isShow
      });
    },
    updateUserInfo: function (a) {
      var t = this;
      app.globalData.util.getUserInfo(function (a) {
        wx.setStorageSync("uid", a.memberInfo.uid), t.hideDialog();
      }, a.detail);
    },
    showtextareaFun: function () {
      var a = this;
      console.log(!a.showtextarea), a.setData({
        showtextarea: !a.showtextarea
      });
    },
    textareashow: function (a) {
      this.setData({
        mapname: a.detail.value
      });
    },
    firstin: function (e) {
      var a = wx.getSystemInfoSync();
      e.setData({
        maxwidth: a.windowWidth,
        maxheight: a.windowHeight
      }), app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          m: "monai_market",
          uid: app.globalData.getuid(),
          r: "sale.index.saleindex"
        },
        cachetime: "0",
        success: function (a) {
          var t = {
            classes: a.data.data.class,
            userphone: "",
            mapname: ""
          };
          a.data.data.user && (t.userphone = a.data.data.user.phone, t.mapname = a.data.data.user.address), e.setData(t);
        }
      });
    },
    selectclassFun: function (a) {
      var t = this.classes[a.detail.value];
      t && this.setData({
        selectclass: t.name,
        classid: t.id
      });
    },
    gearbox: function (a) {
      var t = this.gearboxes[a.detail.value];
      t && this.setData({
        gearboxname: t.name,
        gearboxindex: a.detail.value
      });
    },
    selectbrandFun: function (a) {
      wx.navigateTo({
        url: "/pages/selectscards/index"
      });
    },
    bindDateChange: function (a) {
      console.log(a.detail.value);
      var t = a.detail.value.split("-");
      this.setData({
        selecttime: t[0] + "年" + t[1] + "月",
        selecttime1: a.detail.value
      });
    },
    salefast: function (a) {
      wx.navigateTo({
        url: "/pages/sale_fast/index"
      });
    },
    getPhoneNumber: function (a) {
      console.log(a);
      var t = this;
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          r: "sale.index.userphone",
          m: "monai_market",
          iv: a.detail.iv,
          encryptedData: a.detail.encryptedData
        },
        cachetime: "0",
        success: function (a) {
          t.setData({
            userphone: a.data.data
          });
        }
      });
    },
    tomap: function () {
      var t = this;
      wx.chooseLocation({
        success: function (a) {
          t.setData({
            mapx: a.latitude,
            mapy: a.longitude,
            mapname: a.address + a.name
          });
        },
        fail: function () {
          wx.getSetting({
            success: function (a) {
              a.authSetting["scope.userLocation"] || wx.showModal({
                content: "请允许获取地理位置后再次尝试",
                success: function (a) {
                  a.confirm ? wx.openSetting({
                    success: function (a) {
                      a.authSetting = {
                        "scope.userInfo": !0,
                        "scope.userLocation": !0
                      };
                    },
                    fail: function (a) {
                      console.log(a);
                    }
                  }) : a.cancel && console.log("用户点击取消");
                }
              });
            }
          });
        }
      });
    },
    release: function (a) {
      var t = this,
          e = wx.getStorageSync("uid");

      if (e) {
        t.setData({
          buttscan: !0
        });
        var i = t.classid;
        if ("" == i || "undefined" == i || "请选择车辆类型" == i) return app.globalData.util.message({
          title: "请选择车辆类型",
          type: "error"
        }), void t.setData({
          buttscan: !1
        });
        var s = t.brandid;
        if ("" == s || "undefined" == s || "请选择车辆品牌" == s) return app.globalData.util.message({
          title: "请选择车辆品牌",
          type: "error"
        }), void t.setData({
          buttscan: !1
        });
        var n = t.selecttime;
        if ("请选择出厂年份" == n || "undefined" == n || "" == t.selecttime1) return app.globalData.util.message({
          title: "请选择出厂年份",
          type: "error"
        }), void t.setData({
          buttscan: !1
        });
        var r = a.detail.value.carname;
        if ("" == r || "undefined" == r) return app.globalData.util.message({
          title: "请填写汽车名称",
          type: "error"
        }), void t.setData({
          buttscan: !1
        });
        var u = a.detail.value.kilometre;
        if ("" == u || "undefined" == u) return app.globalData.util.message({
          title: "请填写公里数",
          type: "error"
        }), void t.setData({
          buttscan: !1
        });
        var d = a.detail.value.price;
        if ("" == d || "undefined" == d) return app.globalData.util.message({
          title: "请填写价格",
          type: "error"
        }), void t.setData({
          buttscan: !1
        });
        var c = a.detail.value.exhaust;
        if ("" == c || "undefined" == c) return app.globalData.util.message({
          title: "请填写排气量",
          type: "error"
        }), void t.setData({
          buttscan: !1
        });
        var o = t.gearboxindex;
        if ("" == o || "undefined" == o) return app.globalData.util.message({
          title: "请选择变速箱类型",
          type: "error"
        }), void t.setData({
          buttscan: !1
        });
        var l = a.detail.value.phone;
        if ("" == l || "undefined" == l) return app.globalData.util.message({
          title: "请填写联系电话，或点击电话授权",
          type: "error"
        }), void t.setData({
          buttscan: !1
        });
        var m = a.detail.value.address;
        if ("" == m || "undefined" == m) return app.globalData.util.message({
          title: "请填写车辆地址",
          type: "error"
        }), void t.setData({
          buttscan: !1
        });
        var p = t.carsimgs;
        if (p.length < 3) return app.globalData.util.message({
          title: "请至少添加3个车辆图文",
          type: "error"
        }), void t.setData({
          buttscan: !1
        });
        var g = {
          r: "sale.index.insale",
          m: "monai_market",
          fomid: a.detail.formId,
          uid: e,
          classid: i,
          brand: s,
          exhaust: c,
          times: n,
          times1: t.selecttime1,
          kilometre: u,
          price: d,
          gearboxindex: o,
          phone: l,
          carname: r,
          address: m,
          carimgs: p,
          mapx: t.mapx,
          mapy: t.mapy
        };
        app.globalData.util.request({
          url: "entry/wxapp/Api",
          data: g,
          cachetime: "0",
          success: function (a) {
            a && a.data && 405 == a.data.message ? t.setData({
              isShow: !0,
              buttscan: !1
            }) : a && a.data && a.data.data && wx.showModal({
              content: "提交成功，快去上架吧",
              cancelText: "继续添加",
              confirmText: "去上架",
              success: function (a) {
                a.confirm ? (t.cleardetail(), wx.navigateTo({
                  url: "/pagesA/salelist/index"
                })) : a.cancel && (t.setData({
                  buttscan: !1
                }), wx.reLaunch({
                  url: "/pages/sale/index"
                }));
              }
            });
          },
          fail: function (a) {
            t.setData({
              buttscan: !1
            });
          }
        });
      } else t.setData({
        isShow: !0
      });
    },
    cleardetail: function () {
      this.setData({
        selectclass: "请选择分类",
        selectbrand: "请选择品牌",
        selecttime: " 请选择出厂年份",
        selecttime1: "",
        vehicletime: "请选择上牌日期",
        vehicletime1: "",
        insurance: "请选择保险到期时间",
        insurance1: "",
        gearboxname: "请选择变速箱类型",
        gearboxindex: 0,
        mapx: "",
        mapy: "",
        carimgs: [],
        textareaname: "请输入车辆信息介绍",
        textareainput: "",
        carsimgs: [{
          img: "",
          intro: "",
          imgshort: ""
        }],
        brandid: "",
        brandname: "",
        classid: "",
        defult_value: ""
      });
    },
    upnotice: function (a) {
      this.noticeindex = a.detail.current;
    },
    retonotice: function () {
      var a = this.notice[this.noticeindex];
      a && a.id && wx.navigateTo({
        url: "/pages/index/index?id=" + a.id
      });
    },
    delcarsimgs: function (a) {
      var t = a.currentTarget.dataset.index,
          e = this.carsimgs;
      if (0 == t && 1 < e.length && ("" != e[t].img || "" != e[t].intro)) return "" != e[t].img && app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          m: "monai_sharing",
          uid: app.globalData.getuid(),
          imgurl: e[t].img,
          r: "Upimg.delImg"
        }
      }), e.splice(t, 1), void this.setData({
        carsimgs: e
      });
      0 != t && ("" != e[t].img && app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          m: "monai_sharing",
          uid: app.globalData.getuid(),
          imgurl: e[t].img,
          r: "Upimg.delImg"
        }
      }), e.splice(t, 1), this.setData({
        carsimgs: e
      }));
    },
    upcarsimgs: function (a) {
      var t = this,
          e = a.currentTarget.dataset.index,
          i = t.carsimgs;
      "" != i[e].img && app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          m: "monai_sharing",
          uid: app.globalData.getuid(),
          imgurl: i[e].img,
          r: "Upimg.delImg"
        }
      }), 9 <= i.length ? app.globalData.util.message({
        title: "您添加的图文已经很多了",
        type: "error"
      }) : (t.upcarindex = e, app.globalData.upimgs({
        scannums: 1,
        filename: "cars",
        success: function (a) {
          t.addinimgs(a[0]);
        }
      }));
    },
    addinimgs: function (a) {
      var t = this.carsimgs,
          e = this.upcarindex;
      t[e].img = a.all, t[e].imgshort = a.short, this.setData({
        carsimgs: t
      });
    },
    showimgs: function (a) {
      var t = a.currentTarget.dataset.index,
          e = this.carsimgs;
      wx.previewImage({
        current: e[t].img,
        urls: [e[t].img]
      });
    },
    upthiscarsimgs: function (a) {
      var t = a.currentTarget.dataset.index,
          e = this.carsimgs;
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          m: "monai_sharing",
          uid: app.globalData.getuid(),
          imgurl: e[t].img,
          r: "Upimg.delImg"
        }
      }), e[t].img = "", this.setData({
        carsimgs: e
      });
    },
    setthisintro: function (a) {
      var t = a.currentTarget.dataset.index;
      this.carsimgs[t].intro = a.detail.value;
    },
    addcarsimgs: function () {
      var a = this.carsimgs;
      a.push({
        img: "",
        intro: "",
        imgshort: ""
      }), this.setData({
        carsimgs: a
      });
    },
    upimg: function () {
      var e = this,
          i = e.carimgs;
      if (9 <= i.length) app.globalData.util.message({
        title: "您上传的图片已经很多了",
        type: "error"
      });else {
        i.length;
        app.globalData.upimgs({
          scannums: 1,
          filename: "cars",
          success: function (a) {
            var t = i.concat(a);
            e.setData({
              carimgs: t
            });
          }
        });
      }
    },
    delimg: function (a) {
      console.log(a.currentTarget.dataset.index);
      var t = a.currentTarget.dataset.index,
          e = this.carimgs,
          i = e[t].all;
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          m: "monai_sharing",
          uid: app.globalData.getuid(),
          imgurl: i,
          r: "Upimg.delImg"
        }
      }), e.splice(t, 1), this.setData({
        carimgs: e
      });
    },

    formReset() {
      console.log("占位：函数 formReset 未声明");
    }

  }
};
</script>
<style>
@import "./index.css";
</style>